package com.abvnet.project.water.project.service;

import com.abvnet.common.exception.BusinessException;
import com.abvnet.common.utils.DateUtils;
import com.abvnet.common.utils.security.ShiroUtils;
import com.abvnet.common.utils.text.Convert;
import com.abvnet.project.water.project.domain.MicroBar;
import com.abvnet.project.water.project.domain.ProjectLog;
import com.abvnet.project.water.project.domain.ProjectLogMicro;
import com.abvnet.project.water.project.domain.WaterChart;
import com.abvnet.project.water.project.mapper.ProjectLogMapper;
import com.abvnet.project.water.project.mapper.ProjectLogMicroMapper;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * 项目日志 服务层实现
 *
 * @author duj
 * @date 2019-07-19
 */
@Service
public class ProjectLogServiceImpl implements IProjectLogService {
    @Autowired
    private ProjectLogMapper projectLogMapper;

    @Autowired
    private ProjectLogMicroMapper projectLogMicroMapper;

    /**
     * 查询项目日志信息
     *
     * @param logId 项目日志ID
     * @return 项目日志信息
     */
    @Override
    public ProjectLog selectProjectLogById(Long logId) {
        return projectLogMapper.selectProjectLogById(logId);
    }

    @Override
    public ProjectLog selectProjectLogForApi(Long logId) {
        ProjectLog pl = projectLogMapper.selectProjectLogById(logId);
        pl.setMicroList(projectLogMicroMapper.selectProjectLogMicroListByLogId(logId));
        return pl;
    }

    @Override
    public List<ProjectLogMicro> selectProjectLogMicroByLogId(Long logId) {
        return projectLogMicroMapper.selectProjectLogMicroByLogId(logId);
    }

    /**
     * 查询项目日志列表
     *
     * @param projectLog 项目日志信息
     * @return 项目日志集合
     */
    @Override
    public List<ProjectLog> selectProjectLogList(ProjectLog projectLog) {
        return projectLogMapper.selectProjectLogList(projectLog);
    }

    @Override
    public List<ProjectLog> selectCustomerProjectLogList(ProjectLog projectLog) {
        return projectLogMapper.selectCustomerProjectLogList(projectLog);
    }

    @Override
    public List<ProjectLog> selectProjectLogListForApi(ProjectLog projectLog) {
        return projectLogMapper.selectProjectLogListForApi(projectLog);
    }

    @Override
    public WaterChart selectProjectLogListForData(ProjectLog projectLog) {
        List<ProjectLog> list = projectLogMapper.selectProjectLogListForData(projectLog);
        WaterChart wc = new WaterChart();
        if (CollectionUtils.isEmpty(list)) {
            return wc;
        }
        List<String> dateList = Lists.newArrayList();
        List<Integer> inIntakeList = Lists.newArrayList();
        List<Integer> outIntakeList = Lists.newArrayList();
        List<BigDecimal> inCodList = Lists.newArrayList();
        List<BigDecimal> outCodList = Lists.newArrayList();
        List<BigDecimal> inNh3NList = Lists.newArrayList();
        List<BigDecimal> outNh3NList = Lists.newArrayList();
        List<BigDecimal> inTotalNList = Lists.newArrayList();
        List<BigDecimal> outTotalNList = Lists.newArrayList();
        List<BigDecimal> inTotalPList = Lists.newArrayList();
        List<BigDecimal> outTotalPList = Lists.newArrayList();
        List<BigDecimal> inPhList = Lists.newArrayList();
        List<BigDecimal> outPhList = Lists.newArrayList();
        List<BigDecimal> inSv30List = Lists.newArrayList();
        List<BigDecimal> outSv30List = Lists.newArrayList();
        List<BigDecimal> inDoList = Lists.newArrayList();
        List<BigDecimal> outDoList = Lists.newArrayList();
        List<Integer> inTpList = Lists.newArrayList();
        List<Integer> outTpList = Lists.newArrayList();
        for (ProjectLog l : list) {
            dateList.add(DateUtils.parseDateToStr(l.getLogDate(), "M.dd"));
            inIntakeList.add(l.getInIntake());
            outIntakeList.add(l.getOutIntake());
            inCodList.add(l.getInCod());
            outCodList.add(l.getOutCod());
            inNh3NList.add(l.getInNh3N());
            outNh3NList.add(l.getOutNh3N());
            inTotalNList.add(l.getInTotalN());
            outTotalNList.add(l.getOutTotalN());
            inTotalPList.add(l.getInTotalP());
            outTotalPList.add(l.getOutTotalP());
            inPhList.add(l.getInPh());
            outPhList.add(l.getOutPh());
            inSv30List.add(l.getInSv30());
            outSv30List.add(l.getOutSv30());
            inDoList.add(l.getInDo());
            outDoList.add(l.getOutDo());
            inTpList.add(l.getInTp());
            outTpList.add(l.getOutTp());
        }
        wc.setInIntakeList(inIntakeList);
        wc.setOutIntakeList(outIntakeList);
        wc.setDateList(dateList);
        wc.setInCodList(inCodList);
        wc.setOutCodList(outCodList);
        wc.setInNh3NList(inNh3NList);
        wc.setOutNh3NList(outNh3NList);
        wc.setInTotalNList(inTotalNList);
        wc.setOutTotalNList(outTotalNList);
        wc.setInTotalPList(inTotalPList);
        wc.setOutTotalPList(outTotalPList);
        wc.setInPhList(inPhList);
        wc.setOutPhList(outPhList);
        wc.setInSv30List(inSv30List);
        wc.setOutSv30List(outSv30List);
        wc.setInDoList(inDoList);
        wc.setOutDoList(outDoList);
        wc.setInTpList(inTpList);
        wc.setOutTpList(outTpList);
        return wc;
    }

    @Override
    public MicroBar selectProjectLogMicroListForData(ProjectLog projectLog) {
        List<ProjectLogMicro> list = projectLogMicroMapper.selectProjectLogMicroListForData(projectLog);
        MicroBar mb = new MicroBar();
        if (CollectionUtils.isEmpty(list)) {
            return mb;
        }
        List<String> microNameList = Lists.newArrayList();
        List<Integer> microDosageList = Lists.newArrayList();
        for (ProjectLogMicro m : list) {
            microNameList.add(m.getMicrobialName());
            microDosageList.add(m.getMicroDosage());
        }
        mb.setMicroNameList(microNameList);
        mb.setMicroDosageList(microDosageList);
        return mb;
    }

    /**
     * 新增项目日志
     *
     * @param projectLog 项目日志信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertProjectLog(ProjectLog projectLog) {
        int rows = projectLogMapper.insertProjectLog(projectLog);
        projectLog.getMicroList().forEach(m -> m.setLogId(projectLog.getLogId()));
        projectLogMicroMapper.batchProjectLogMicro(projectLog.getMicroList());
        return rows;
    }

    /**
     * 修改项目日志
     *
     * @param projectLog 项目日志信息
     * @return 结果
     */
    @Override
    @Transactional
    public int updateProjectLog(ProjectLog projectLog) {
        projectLogMicroMapper.deleteProjectLogMicroByLogId(projectLog.getLogId());
        projectLog.getMicroList().forEach(m -> m.setLogId(projectLog.getLogId()));
        projectLogMicroMapper.batchProjectLogMicro(projectLog.getMicroList());
        return projectLogMapper.updateProjectLog(projectLog);
    }

    /**
     * 删除项目日志对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteProjectLogByIds(String ids) {
        return projectLogMapper.deleteProjectLogByIds(Convert.toStrArray(ids));
    }

    /**
     * 导入项目日志
     * @param projectLogList
     * @return
     */
    @Override
    public String importProjectLog(Long projectId, List<ProjectLog> projectLogList) {
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
        for (ProjectLog projectLog : projectLogList) {
            try {
                projectLog.setProjectId(projectId);
                projectLog.setLogDate(sdf.parse(projectLog.getLogDateStr()));
                projectLog.setUserId(ShiroUtils.getUserId());
                projectLog.setUserName(ShiroUtils.getLoginName());
                projectLog.setCreateBy(ShiroUtils.getLoginName());
                projectLog.setCreateTime(new Date());
                projectLogMapper.insertProjectLog(projectLog);
                successNum++;
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "日志数据, 导入失败：";
                failureMsg.append(msg + e.getMessage());
                //log.error(msg, e);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new BusinessException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}